<?php
namespace app\service\common;


use support\Response;

class PhoneCity {


    /**
     * 手机号码归属地查询
     * @param int $phone 手机号码
     * @return array|Response
     */
    public function get_phone_city(int $phone) {
        if (empty($phone)) {
            return errorMessage("手机号码为空");
        }
        if (!preg_match("/^1[23456789]\d{9}$/", $phone)) {
            return errorMessage("手机号格式错误");
        }
        $prefix = substr($phone, 0, 3);
        $area_code = substr($phone, 3, 4);
        $path = public_path("/data/mobile/".$prefix. '.php');
        if (!file_exists($path)) {
            return errorMessage("手机号识别失败，更新归属地址库");
        }
        $list = require $path;
        $region = config('phone_city');
        if (!isset($list[$area_code])) {
            return errorMessage("手机号识别失败，更新归属地址库");
        }

        $data = $list[$area_code];
        $isp="";
        switch ($data[0]) {
            case 1: $isp = '移动';break;
            case 2: $isp = '联通';break;
            case 3: $isp = '电信';break;
        }
        $region_data = $region[$data[1]];

        return [
            'segment_no' => $prefix,
            'area_code' => $area_code,
            'isp' => $isp,
            'province' => $region_data['a'],
            'city' => $region_data['b']
        ];
    }
}
